package cn.gov.msa.collection;

import org.apache.log4j.Logger;

import cn.gov.msa.model.SuperInspecRecord;
import cn.gov.msa.util.PropertiesTools;
import cn.gov.msa.util.WebServiceTools;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 将水工作业与行政备案数据传送给中创
 * @author junxin
 *
 */
public class TransportDataService {
	
	private static Logger logger = Logger.getLogger(TransportDataService.class);
	
	/**
	 * 水上水下活动（新增/修改）数据接收接口
	 * @param param 
	 * @param superInspecRecord 
	 * @return {"applyid":"ACAAFE0A-335A-436F-8BC3-CA7379A9EFAC","result":"0:失败/1:成功","resultinfo":"1:数据接收成功/0:失败原因"}
	 */
	public static String sendWatersWork(String param, SuperInspecRecord superInspecRecord) {
	    JSONObject sendWatersWork = new JSONObject();
		try {
			JSONObject sendData = JSONObject.fromObject(param);
			
			JSONObject business = sendData.getJSONObject("business");
			
			JSONObject hydwork = new JSONObject();
			hydwork.put("applyid", sendData.getJSONObject("apply").optString("applyid"));
			hydwork.put("applyno", sendData.getJSONObject("apply").optString("applyno"));
			hydwork.put("applytime", sendData.getJSONObject("apply").optString("applytime"));
			hydwork.put("orgcode", sendData.getJSONObject("apply").optString("orgcode"));
			hydwork.put("workname", business.optString("workname"));
			hydwork.put("worktypecode", business.optString("worktypecode"));
			hydwork.put("workbegintime", business.optString("workbegintime"));
			hydwork.put("workendtime", business.optString("workendtime"));
			hydwork.put("buildunitname", business.optString("buildunitname"));
			hydwork.put("workunitname", business.optString("workunitname"));
			hydwork.put("workstep", business.optString("workstep"));
			hydwork.put("workarea", business.optString("workarea"));
			hydwork.put("workpositioncode", business.optString("workposition"));
			hydwork.put("workmethodcode", business.optString("workmethodcode"));
			
			if(sendData.has("ship")){
				JSONArray ship = sendData.getJSONArray("ship");
				StringBuilder workships = new StringBuilder();
				for (int i = 0; i < ship.size(); i++) {
					workships.append(ship.getJSONObject(i).optString("shipname")).append(",");
				}
				if(workships.length() > 0){
					workships.substring(0, workships.length()-1);
				}
				hydwork.put("workship", workships.toString());
			}
			
			// {\"hydwork\": {\"applyid\": \"ACAAFE0A-335A-436F-8BC3-CA7379A9EFAC\",\"applyno\": \"DZZW201800109347\",\"workname\": \"项目名称\",\"worktypecode\": \"03\",\"workbegintime\": \"2018-05-23 12:35:31\",\"workendtime\": \"2018-05-23 12:35:31\",\"buildunitname\": \"建设单位\",\"workunitname\": \"施工单位\",\"workstep\": \"施工作业程序\",\"workarea\": \"地点范围\",\"workpositioncode\": \"1\",\"workmethodcode\": \"4\",\"applytime\": \"2018-08-23 12:35:31 \",\"orgcode\": \"060100\",\"workship\": \"测试01,测试02\"}}
			sendWatersWork = new JSONObject();
			sendWatersWork.put("hydwork", hydwork);

			superInspecRecord.setSendcontent(sendWatersWork.toString());
			
		} catch (Exception e1) {
			logger.error("水上水下活动转换异常.", e1);
			return "{\"result\":\"0\",\"info\":\"数据转换异常.:"+e1.getMessage()+"\"}";
		}
		
		
		String wsdl= PropertiesTools.readProperties("hydwork.test.url");
		String method = PropertiesTools.readProperties("hydwork.test.method");
	    String result = "{\"result\":\"1\",\"info\":\"数据接收成功\"}";		
	    try {
			result = WebServiceTools.getDataByUrl(wsdl, method, sendWatersWork.toString());
		} catch (Exception e) {
			logger.error("水上水下活动数据推送接口异常.", e);
			result = "{\"result\":\"0\",\"info\":\"水上水下活动数据推送接口异常:"+e.getMessage()+"\"}";
		}
		return result;
	}
	
	/**
	 * 行政备案（新增/修改）数据接收接口
	 * @param itemno 1015:内河通航水域安全作业备案/2002:船舶在港区水域内安全作业备案
	 * @param param 
	 * @param superInspecRecord 
	 * @return {"applyid":"ACAAFE0A-335A-436F-8BC3-CA7379A9EFAC","result":"0:失败/1:成功/2:参数有误","resultinfo":"1:数据接收成功/0:失败原因"}
	 */
	public static String sendAdministrativeRecord (String itemno, String param, SuperInspecRecord superInspecRecord) {
		String wsdl = PropertiesTools.readProperties("adminrecord.test.url");
		String method = "";
		JSONObject pushAdminRecData = new JSONObject();
		if("1015".equals(itemno)){
			try {
				JSONObject sendData = JSONObject.fromObject(param);
				
				JSONObject business = sendData.getJSONObject("business");
				business.remove("otherbehaviorcode");
				business.remove("otherbehavior");
				
				if(sendData.has("ship")){
					JSONArray ship = sendData.getJSONArray("ship");
					StringBuilder workships = new StringBuilder();
					for (int i = 0; i < ship.size(); i++) {
						workships.append(ship.getJSONObject(i).optString("shipname")).append(",");
					}
					if(workships.length() > 0){
						workships.substring(0, workships.length()-1);
					}
					business.put("workship", workships.toString());
				}
				
				// {\"rivershipwork\":{\"applyid\":\"134d0d83-be7a-43bb-bf3d-9c39d6da1222\",\"business\":\"作业项目\",\"wordaddress\":\"作业地点\",\"workbegintime\":\"2018-05-23 12:35:31 \",\"workendtime\":\"2018-05-23 12:35:31 \",\"workunitname\":\"作业单位\",\"workship\":\"测试1\",\"workcontactperson \":\"作业单位联系人\",\"workcontactphone\":\"作业单位联系电话 \"}}
				pushAdminRecData = new JSONObject();
				pushAdminRecData.put("rivershipwork", business);

				superInspecRecord.setSendcontent(pushAdminRecData.toString());
			} catch (Exception e1) {
				logger.error("内河通航水域安全作业备案转换异常.", e1);
				return "{\"result\":\"0\",\"info\":\"数据转换异常.:"+e1.getMessage()+"\"}";
			}
			
			method = PropertiesTools.readProperties("rivershipwork.test.method");
			
		} else if("2002".equals(itemno)){
			
			try {
				JSONObject sendData = JSONObject.fromObject(param);
				
				JSONObject business = sendData.getJSONObject("business");
				
				if(sendData.has("ship")){
					JSONArray ship = sendData.getJSONArray("ship");
					StringBuilder workships = new StringBuilder();
					for (int i = 0; i < ship.size(); i++) {
						workships.append(ship.getJSONObject(i).optString("shipname")).append(",");
					}
					if(workships.length() > 0){
						workships.substring(0, workships.length()-1);
					}
					business.put("workship", workships.toString());
				}
				
				// {\"portShipWork\":{\"applyid\":\"134d0d83-be7a-43bb-bf3d-9c39d6da1222\",\"business\":\"作业项目\",\"wordaddress\":\"作业地点\",\"workbegintime\":\"2018-05-23 12:35:31 \",\"workendtime\":\"2018-05-23 12:35:31 \",\"workunitname\":\"作业单位\",\"workship\":\"测试1\",\"workcontactperson \":\"作业单位联系人\",\"workcontactphone\":\"作业单位联系电话 \"}}
				pushAdminRecData = new JSONObject();
				pushAdminRecData.put("portshipwork", business);

				superInspecRecord.setSendcontent(pushAdminRecData.toString());
			} catch (Exception e1) {
				logger.error("船舶在港区水域内安全作业备案转换异常.", e1);
				return "{\"result\":\"0\",\"info\":\"数据转换异常.:"+e1.getMessage()+"\"}";
			}
			
			method = PropertiesTools.readProperties("portshipwork.test.method");
			
		} else {
			return "{\"result\":\"2\",\"info\":\"无此接口,itemno:"+itemno+"\"}";
		}
		
	    String result = "{\"result\":\"1\",\"info\":\"数据接收成功\"}";
		try {
			result = WebServiceTools.getDataByUrl(wsdl, method, pushAdminRecData.toString());
		} catch (Exception e) {
			logger.error("行政备案数据推送接口异常.", e);
			result = "{\"result\":\"0\",\"info\":\"行政备案数据推送接口异常:"+e.getMessage()+"\"}";
		}
		return result;
	}

}
